#!/bin/bash

cpDestOpts=--preserve=timestamps
Prefix=/my

if [[ "$PhotivoRepoPath"  == "" ]];            then printf "PhotivoRepoPath not set!\n"    ; exit 1 ; fi
if [[ "$PhotivoBuildPath" == "" ]];            then printf "PhotivoBuildPath not set!\n"   ; exit 1 ; fi
if [[ "$GccArch"       == "" ]];               then printf "GccArch not set!\n"            ; exit 1 ; fi
if [[ ($GccArch != 64) && ($GccArch != 32) ]]; then printf "Wrong GccArch (${GccArch})!\n" ; exit 1 ; fi

Result () {
  if [[ $1 == 0 ]]; then
    printf "\e[0;32mUpdating DLLs succeeded!\e[00m\n"
    exit 0
  else
    printf "\e[1;31mUpdating DLLs failed!\e[00m\n"
    exit 1
  fi
}

#=============================================================================

if [[ "$1" != "" ]]; then
  TheBindir="$1"
else
  if [ ! -d "$PhotivoBuildPath" ]; then
    echo "Path to build folder \"$PhotivoBuildPath\" not found."
    exit 1
  fi
  TheBindir="$PhotivoBuildPath"
fi


echo "Updating all DLLs in $TheBindir ... "

if [ ! -d "$TheBindir" ]; then
  mkdir -p "$TheBindir"
else
  rm -f "$TheBindir/*.dll"
fi

if [[ "$GccArch" == "64" ]]; then
  cd /mingw/qt/bin && \
  cp QtCore4.dll \
     QtGui4.dll \
     QtNetwork4.dll \
     $TheBindir $cpDestOpts && \
  cd /mingw/bin && \
  cp libgcc_s_sjlj_64-1.dll \
     libgomp_64-1.dll \
     libstdc++_64-6.dll \
     pthreadGC2_64.dll \
     $TheBindir $cpDestOpts && \
  cd $Prefix/bin && \
  cp lensfun.dll \
     libexpat-1.dll \
     libfreetype-6.dll \
     libglib-2.0-0.dll \
     libintl-8.dll \
     zlib1.dll \
     $TheBindir $cpDestOpts

elif [[ "$GccArch" == "32" ]]; then
  cd /mingw/qt/bin && \
  cp QtCore4.dll \
     QtGui4.dll \
     QtNetwork4.dll \
     $TheBindir $cpDestOpts && \
  cd /mingw/bin && \
  cp libgcc_s_sjlj-1.dll \
     libgomp-1.dll \
     libstdc++-6.dll \
     pthreadGC2.dll \
     $TheBindir $cpDestOpts && \
  cd $Prefix/bin && \
  cp freetype6.dll \
     intl.dll \
     lensfun.dll \
     libexpat-1.dll \
     libglib-2.0-0.dll \
     zlib1.dll \
     $TheBindir $cpDestOpts
fi


if [[ $? == 0 ]]; then
  SrcDir=$Prefix/bin/
  cp ${SrcDir}libexiv2.dll \
     ${SrcDir}libfftw3-3.dll \
     ${SrcDir}libGraphicsMagick-3.dll \
     ${SrcDir}libGraphicsMagick++-3.dll \
     ${SrcDir}libGraphicsMagickWand-2.dll \
     ${SrcDir}libiconv-2.dll \
     ${SrcDir}libjpeg-8.dll \
     ${SrcDir}liblcms2-2.dll \
     ${SrcDir}liblqr-1-0.dll \
     ${SrcDir}libpng15-15.dll \
     ${SrcDir}libtiff-3.dll \
     $TheBindir $cpDestOpts
fi

Result $?
